package com.xmall.order.controller;

import com.xmall.annotation.LoginRequiredFalse;
import com.xmall.aspect.OperateRecord;
import com.xmall.aspect.RepeatSubmit;
import com.xmall.define.base.Result;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.define.dto.order.OrderDto;
import com.xmall.order.define.dto.OrderDtoWithCascade;
import com.xmall.define.dto.product.SkuQuantity;
import com.xmall.order.define.dto.OrderConfirmResponseDto;
import com.xmall.order.define.request.api.OrderConfirmRequest;
import com.xmall.order.define.request.api.OrderSubmitRequest;
import com.xmall.order.service.IOrderService;
import com.xmall.order.service.other.IOrderTradeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/service/order/order")
public class OrderController {

    @Resource
    private IOrderService orderService;

    @Resource
    private IOrderTradeService orderTradeService;

    /**
     * 订单详情
     */
    @LoginRequiredFalse
    @GetMapping("/info/{id}")
    public Result<OrderDto> info(@PathVariable("id") Long id) {
        return Result.success(orderService.info(id));
    }

    /**
     * 订单详情
     */
    @GetMapping("/infoWithCascade/{id}")
    public Result<OrderDtoWithCascade> infoWithCascade(@PathVariable("id") Long id) {
        return Result.success(orderService.infoWithCascade(id));
    }

    /**
     * 库存检查
     */
    @PostMapping("/stockCheck")
    public Result<List<Long>> stockCheck(@Validated @RequestBody List<SkuQuantity> skuQuantityList) {
        return Result.success(orderService.stockCheck(skuQuantityList));
    }

    /**
     * 订单确认
     */
    @PostMapping("/confirm")
    public Result<OrderConfirmResponseDto> confirm(@Validated @RequestBody OrderConfirmRequest request) {
        return Result.success(orderService.confirm(request));
    }

    /**
     * 提交订单
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @RepeatSubmit
    @PostMapping("/submit")
    public Result<Long> submit(@Validated @RequestBody OrderSubmitRequest request) {
        return Result.success(orderService.submit(request));
    }

    /**
     * 支付宝手机网站支付
     */
    @PostMapping("/aliMobileWapPay")
    public Result<String> aliMobileWapPay(
            @RequestParam("orderId") Long orderId,
            @RequestParam("returnUrl") String returnUrl,
            HttpServletResponse response) {
        String result = orderTradeService.aliMobileWapPay(orderId, returnUrl, response);
        return Result.success(result);
    }

    /**
     * 确认收货
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PostMapping("/takeDelivery/{id}")
    public Result receiveOrder(@PathVariable("id") Long id) {
        orderService.takeDelivery(id);
        return Result.success();
    }

    /**
     * 删除订单
     */
    @OperateRecord(type = OperateTypeEnum.DELETE)
    @PostMapping("/deleteOrder/{id}")
    public Result deleteOrder(@PathVariable("id") Long id) {
        orderService.deleteOrder(id);
        return Result.success();
    }

}
